var pnum=1;   
$(document).ready(function(){
	   initData(1);
	   $("#addData").click(addProject);
   });
   
   //初始化数据
   var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/projectCenter/businessdata/projectList",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data.list != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"project_code": "项目编号"
		          			      	,"project_name":"项目名称"
		          			      	,"project_user_name":"项目负责人"
		          			      	,"project_state":"项目状态"
		          			      	,"project_design_phase_name":"设计阶段"
		          			      	,"project_design_content_name":"设计内容"
		          			      	,"project_design_major_name":"设计专业"
		          			      	,"create_time":"创建时间"
		          			      	,"update_time":"更新时间"
		          			      	,"operation":"操作"
		            			},
								"editField":[{"field":"project_name","type":"text"},
											 {"field":"project_state","type":"select","selectArray":[{"id":1,"value":"是"},{"id":0,"value":"否"}]}
											 ],
		            			"titleCheckbox":false,
								"btn":[{"event":"updateObj","name":"编辑"},
								       {"event":"projectDesign","name":"项目设计"},
								       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"initData");
	        	}

	        },
	        error: function () {
	        }
	    });
 }
   var celledit = function (obj,key,value,that,type,val) {
		if (type=='text') {
            var t_name = $.trim($("#t_name").val());
            if(!$(that).is('.input')){
                $(that).addClass('input').html('<input type="text" id="'+key+'" value="'+ value +'" />').find('input').focus().blur(function(){
                    if (value!=$("#"+key).val()) {
                        $.ajax({
                            url:base+"/projectCenter/commonupdate/updateOneField/",
                            data:{"record_id":obj.id,"t_name":t_name,"field_name":key,"value":$("#"+key).val()},
                            success:function(data,status){
                                initData(pnum);
                            }
                        });
                    }
                    $(that).removeClass('input').html($("#"+key).val());
                });
            }
		} else if (type=='select') {
            var t_name = $.trim($("#t_name").val());
            var str = val.split("-");
            var soption = "";
            for (var i=0;i<str.length;i++) {
            	if (""!=str[i]) {
                    if (value==str[i].split("_")[1]) {
                        soption+="<option value='"+str[i].split("_")[0]+"' selected>"+str[i].split("_")[1]+"</option>";
                    } else {
                        soption+="<option value='"+str[i].split("_")[0]+"'>"+str[i].split("_")[1]+"</option>";
                    }
				}
			}
            if(!$(that).is('.select')){
            	var html = "<select id='"+key+"' name='"+key+"' style='display:block;'>"+soption+"</select>";
                $(that).addClass('select').html(html).find('select').focus().blur(function(){
                    if (value!=$("#"+key).val()) {
                        $.ajax({
                            url:base+"/projectCenter/commonupdate/updateOneField/",
                            data:{"record_id":obj.id,"t_name":t_name,"field_name":key,"value":$("#"+key).val()},
                            success:function(data,status){
                                initData(pnum);
                            }
                        });
                    }
                    $(that).removeClass('select').html($("#"+key).val());
                });
            }
		}

   }
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/projectCenter/businessdata/delProjectById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(1);
			   }
		   });
	   });
   }
   
   var initDesignMajor = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
   		  	function(data,status){
     		    	if(data!=''){
   	      		  	 var optionstring = "";
   	                 $.each(data.list, function(i,item){
   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
   	                });
   	                $("#select").html('<option value=""></option>' + optionstring);
   	      		}else{
   	      		  	$("#select").html('<option value=""></option>');
   	      		}
     		    form.render(); 
  });
  }
   
   //项目设计
   var projectDesign = function(obj){
	   var url = base+"/projectCenter/businessdata/projectDesign/"+obj.id;
	   window.location.href = url;
   }  
   
   //返回首页
   var go_back = function(){
	    //location.href = base+"/projectCenter/projects";
	    location.href = base+"/resource/index";
   }
   
   //更新项目
   var updateObj = function(obj){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "id":"id",
						    "project_code":"项目编号",
					      	"project_name": "项目名称",
					      /*	"project_short_name":"项目简称",*/
					      	"project_design_phase_name":"所属设计阶段",
					      	"project_design_major_name":"所属设计专业",
					      	"project_design_content_name":"所属设计内容"
					},"text":{
						    "id":obj.id
						    ,"project_code":obj.project_code
						    ,"project_name":obj.project_name
							/*,"project_short_name":obj.project_short_name*/
							,"project_design_phase_name":obj.project_design_phase_name
							,"project_design_major_name":obj.project_design_major_name
							,"project_design_content_name":""	
					},"input":{
						    "id":"hidden"
						    ,"project_code":"text"
						    ,"project_name":"text"
							/*,"project_short_name":"text"*/
							,"project_design_phase_name":"text_readonly"
							,"project_design_major_name":"text_readonly"
							,"project_design_content_name":"radio"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
		initDesignContent(obj.project_design_major_id,obj.id);//回显设计内容
   }
   
   //添加项目
   var addProject = function(){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "project_code":"项目编号",
					      	"project_name": "项目名称",
					      	"project_design_phase_id":"所属设计阶段",
					      	"project_design_major_id":"所属设计专业",
					      	"project_design_content_id":"所属设计内容"
					},"text":{
						    "project_code":""
						    ,"project_name":""
							,"project_design_phase_id":""
							,"project_design_major_id":""
							,"project_design_content_id":""	
					},"input":{
						    "project_code":"text"
						    ,"project_name":"text"
							,"project_design_phase_id":"select"
							,"project_design_major_id":"new_select"
							,"project_design_content_id":"radio"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
		initPhase();
		initDesignMajor();
		chanageMajorselect();
   }
   
   var initPhase = function(project_design_phase_id){
	   $.post(base+"/dataCenter/businessdata/findDesignPhaseData",{},
	   		  	function(data,status){
    		    	if(data != null && data.list != null){
  	      		  	 var optionstring = "";
  	                 $.each(data.list, function(i,item){
  	                	if(item.id == project_design_phase_id){
  	                		optionstring += "<option selected='selected' value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
  	                	}else{
  	                		optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
  	                	}
  	                });
  	                $("select[name='project_design_phase_id']").html('<option value=""></option>' + optionstring);
  	      		}else{
  	      		  	$("select[name='project_design_phase_id']").html('<option value=""></option>');
  	      		}
    		    form.render(); 
	  });
   }
   
   var initDesignMajor = function(project_design_major_id){
	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
   		  	function(data,status){
     		    	if(data!=''){
   	      		  	 var optionstring = "";
   	                 $.each(data.list, function(i,item){
   	                	if(project_design_major_id == item.id){
   	                		optionstring += "<option selected='selected' value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
   	                	}else{
   	                		optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
   	                	}
   	                });
   	                $("select[name='project_design_major_id']").html('<option value=""></option>' + optionstring);
   	      		}else{
   	      		  	$("select[name='project_design_major_id']").html('<option value=""></option>');
   	      		}
     		    form.render(); 
  });
  }
   
   //监听专业下拉选
   var chanageMajorselect = function(project_id){
   		form.on("select(new_select)",function(value){
   			var major_id = value.value;
   			initDesignContent(major_id,project_id);
   		});
   }
   
   //初始化设计内容
   var initDesignContent = function(major_id,project_id){
	   $.post(base+"/dataCenter/businessdata/findDesignContentByProjectId",{major_id:major_id,project_id:project_id},
	   		  	function(data,status){
    		    	if(data!=''){
  	      		  	 var checkboxString = "";
  	                 $.each(data, function(i,item){
  	                	  	if(item.project_id != null && item.project_id != ""){
	                			checkboxString += " <input name = 'project_design_content_id' title = "+item.project_design_content_name+" type='radio' value = "+item.id+" checked=''>";
	                		}else{
	                			checkboxString += " <input name = 'project_design_content_id' title = "+item.project_design_content_name+" type='radio' value="+item.id+" >";
	                		}
  	                });
	   	                $("#radio").html(checkboxString);
	   	      		}else{
	   	      		  	$("#radio").html("");
	   	      		}
	     		    form.render(); 
	   });
   }

   //数据验证
   var verify_data = function(data){
		if(data.project_code == ""){
			var obj = $("input[name='project_code']");
			obj.focus();
			layer.tips('请填写项目编号!', obj);
			return false;
		}
		if(data.project_name == ""){
			var obj = $("input[name='project_name']");
			obj.focus();
			layer.tips('请填写项目名称!', obj);
			return false;
		}
/*		if(data.project_short_name == ""){
			var obj = $("input[name='project_short_name']");
			obj.focus();
			layer.tips('请填写项目简称!', obj);
			return false;
		}*/
		if(data.project_design_phase_id == ""){
			alert("请选择设计阶段")
			return false;
		}
		if(data.project_design_major_id == ""){
			alert("请选择设计专业!")
			return false;
		}
		return true;
   }
   
   //渲染弹窗
   var layopen = function(content){
		layer.open({
		  	title:'更新项目基本信息'
			,content: content
	    	,area: ['650px', '650px']
	      	,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
				var data = $(layero).find("form").serializeJSON();
				if(verify_data(data)){
					var project_design_content_id;
					var project_design_content_name;
					$.each($('input[type=radio]:checked'),function(index,value){
						project_design_content_id = $(this).val();
						project_design_content_name = $(this).attr("title");
					});
					if(project_design_content_id == "" || project_design_content_id == null){
						alert("请选择设计内容！")
						return false;
					}
				$.post(base+"/projectCenter/businessdata/updateProject",{
					id:data.id,
					project_code:data.project_code,
					project_name:data.project_name,
				/*	project_short_name:data.project_short_name,*/
					project_design_phase_id:data.project_design_phase_id,
					project_design_major_id:data.project_design_major_id,
					project_design_content_id:project_design_content_id,
					project_design_content_name:project_design_content_name
				},function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(1);
		  		 })
				}
			}
	});  
   }
   
   
   //初始化设计内容
   var initMentions = function(major_id , project_id){
	  if(major_id == "" || project_id == ""){
		  return false;
	  }
	  $.post(base+"/projectCenter/businessdata/findDesignContentByProjectId",{major_id:major_id,project_id:project_id},
   		  	function(data,status){
 		    	if(data!=''){
      		  	 var checkboxString = "";
                 $.each(data, function(i,item){
            		if(item.project_id != null && item.project_id != ""){
            			checkboxString += " <input name = "+item.id+" title = "+item.project_design_content_name+" type='checkbox' checked='' >";
            		}else{
            			checkboxString += " <input name = "+item.id+" title = "+item.project_design_content_name+" type='checkbox'>";
            		}
                });
   	                $("#checkbox").html(checkboxString);
   	      		}else{
   	      		  	$("#checkbox").html("");
	   	      		}
	     		    form.render(); 
	        });
	  }
   